//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.wxx.service.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.wxx.common.Const;
import com.wxx.mapper.SysCodeMapper;
import com.wxx.model.SysCode;
import com.wxx.model.sdk.UcpaasJson;
import com.wxx.service.tool.ucpaas.UcpaasService;
import com.wxx.util.AppUtil;
import com.wxx.util.JsonData;
import com.wxx.util.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private SysCodeMapper codeMapper;
    @Autowired
    private UcpaasService ucpaasService;

    @Override
    public Map sendCode(String phoneCode, String phoneNum) throws ParseException {
        //云之讯 同一个手机号1分钟内不能超过2条，24小时内验证类不能超过8条，通知类不能超过10条

        //验证一分钟内只能发2条短信
        String date = AppUtil.dateStr(2, new Date());
        Integer minNum = codeMapper.findCountByDateAndPhone(date,phoneCode,phoneNum);
        if (minNum >= Const.SEND_CODE_MIN_COUNT) {
            return JsonData.returnMap(100,Const.SEND_CODE_MIN_ERROR);
        }

        //验证一天发送的数量，超过提示
        String today = DateUtil.today();
        Integer num = codeMapper.findCountByDateAndPhone(today, phoneCode,phoneNum);
        if (num >= Const.SEND_CODE_DAY_COUNT) {
            return JsonData.fail(Const.SEND_CODE_DAY_ERROR);
        }

        //随机6位验证码
        String randomCode = RandomUtil.randomNumbers(6);

        SysCode code = new SysCode();
        code.setId(AppUtil.uuid());
        code.setPhoneCode(phoneCode);
        code.setPhoneNum(phoneNum);
        code.setCode(randomCode);
        code.setCodeStatus(1);
        code.setCreateTime(new Date());
        //设置验证码有效时间
        String dateStr = AppUtil.calDate(5, Const.SEND_CODE_VALID_MIN, new Date());
        code.setValidTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr));
        Integer integer = codeMapper.insertSelective(code);

        if (integer > 0) {
            //使用第三方发短信 （云之讯）
            UcpaasJson ucpaas = new UcpaasJson();
            String replace = phoneCode.replace("+","00");
            ucpaas.setTo(replace + phoneNum);
            ucpaas.setParam(randomCode);
            ucpaas.setTemplateId(PropertiesUtil.getProperty("SEND_CODE_TEMPLATE_ID"));
            Map map = ucpaasService.sendSmsInternational(ucpaas);
            if (!map.get("code").equals(Const.SUCCESS_CODE)) {
                System.out.println("短信发送失败code " + map.get("code"));
                return JsonData.fail("发送失败");
            }
            return JsonData.success(code);
        } else {
            return JsonData.fail("发送失败");
        }
    }
}
